<?php
class session{
	private $sess=array('lifetime'=>0,'path'=>'/','domain'=>'','secure'=>false,'httponly'=>false);
	private $crate=false;
	private $id=false;
	private $gc=5;
	private $name='';
	private $path='';
	public function init($params){
		foreach($params as $key=>$var){
			$this->$key=is_array($this->$key)?array_merge($this->$key,$var):$var;
		}
		$this->path=$this->yum->vars('sys','root').$this->path;
		$this->sess['lifetime']=$this->yum->runtime+$this->sess['lifetime'];
		if(empty($params)||$params['build']){
			$this->CRATE();
		}
	}
	public function CRATE(){
		$this->crate=true;
		session_set_cookie_params($this->sess['lifetime'],$this->sess['path'],$this->sess['domain'],$this->sess['secure'],$this->sess['httponly']);
		session_save_path($this->path);
		session_name($this->name);
		session_start();
		if($this->id!==false){session_id($this->id);}
		if(isset($_COOKIE[$this->name])){
			setcookie($this->name,session_id(),$this->sess['lifetime'],$this->sess['path'],$this->sess['domain'],$this->sess['secure'],$this->sess['httponly']);
		}
	}
    public function SVAR($key,$value){//设置变量
    	if(!$this->crate){$this->CRATE();}
		$_SESSION[$key]=$value;
    }
    public function UNVAR($key){//去除一个变量
		session_unregister($key);
    }
    public function ISVAR($key){//是否注册变量
    	return isset($_SESSION[$key]);
    }
    public function GVAR($key){//得到一个变量
		return isset($_SESSION[$key])?$_SESSION[$key]:false;
    }
    public function GID(){//得到会话ID
		return session_id();
    }
    public function destroy(){//消毁会话
    	if(isset($_COOKIE[$this->name])){
    		if(!$this->crate){
	    		session_save_path($this->path);
				session_name($this->name);
				session_start();
    		}
    		session_destroy();
    		setcookie($this->name,'',-1,$this->sess['path'],$this->sess['domain'],$this->sess['secure'],$this->sess['httponly']);
    	}
		if(mt_rand(0,100)<$this->gc){
			$this->sess_gc();
		}
		return true;
    }
    public function sess_gc(){
		//$this->yum->now-$this->yum->vars('lib','sessexpiration');
    }
    public function ISSESS(){
		if(isset($_COOKIE[$this->name])){
			$this->CRATE();
			return true;
		}
		return false;
    }
}
?>